using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using TVT.UI.EventArguments;
using TVT.UI.Helpers;
using TVT.Common.Helpers;
using TVT.BizLayer.Entities;

namespace TVT.UI
{
    public partial class MatchFinder : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                User user = TVT.BizLayer.Entities.User.Find((Guid)Membership.GetUser().ProviderUserKey);
                UserProfileControlId.Visible = false;
                RequarePin.Visible = false;
                if ((user.UserProfile != null) && (user.UserProfile.EigenFaceDistance == 0))
                {
                    FaceRecognition.Visible = true;
                    ListMatchs.Visible = false;
                }
                else
                {
                    FaceRecognition.Visible = false;
                    ListMatchs.Visible = true;
                }
            }
            FaceRecognition.OnRecognitionFinish += new EventHandler(OnRecognitionFinish);
            ListMatchs.OnContactUser += new EventHandler(ListMatchs_OnContactUser);
            RequarePin.OnPinEntered += new EventHandler(OnPinEntered);
        }

        void ListMatchs_OnContactUser(object sender, EventArgs e)
        {
            Guid guid = ((UserEventArgs)e).IdUser;
            Guid loggedUser = (Guid)Membership.GetUser().ProviderUserKey;
            UserProfileControlId.SetUser(guid);
            UserRelation userRelation = UserRelation.Find(loggedUser, guid);
            if (userRelation.Id != 0 && userRelation.Status == Enumerators.RelationStatus.OnContact)
            {
                UserProfileControlId.Visible = true;
                upUserProfileControl.Update();
            }
            else
            {
                RequarePin.SetUser(guid);
                RequarePin.Visible = true;
                upRequiredPin.Update();
            }
        }

        void OnPinEntered(object sender, EventArgs e)
        {
            Guid guid = ((UserEventArgs)e).IdUser;
            Guid loggedUser = (Guid)Membership.GetUser().ProviderUserKey;
            UserRelation userRelation = new UserRelation() { UserId1 = loggedUser, UserId2 = guid, Status = Enumerators.RelationStatus.OnContact };
            userRelation.Save();

            RequarePin.Visible = false;
            UserProfileControlId.Visible = true;
            upRequiredPin.Update();
            upUserProfileControl.Update();
        }

        void OnRecognitionFinish(object souce, EventArgs e)
        {
            FaceRecognition.Visible = false;
            ListMatchs.Visible = true;
            upFaceRecognition.Update();
            upListMatchs.Update();
        }

    }
}
